package com.view;

import com.run.UserRun;
import com.model.User;
import com.util.DBUtil;
import com.util.StringUtil;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;

public class log_in_window {
    private JFrame jFrame = new JFrame("欢迎使用管理系统");
    private JLabel label = new JLabel("欢迎使用图书管理系统");
    private JLabel UsernameLable = new JLabel("账号:");
    private JLabel PasswordLable = new JLabel("密码:");
    private JLabel identityLabel = new JLabel("身份:");
    private JTextField  UsernameText = new JTextField(30);
    private JPasswordField PasswordText = new JPasswordField(30);
    private JRadioButton identityButton1 = new JRadioButton("学生",true);
    private JRadioButton identityButton2 = new JRadioButton("管理员");
    private ButtonGroup identityGroup = new ButtonGroup();
    private JButton logIn = new JButton("登录");
    private JButton register = new JButton("注册");
    private Box mainBox = Box.createVerticalBox();
    private Box lableBox = Box.createHorizontalBox();
    private Box usernameBox = Box.createHorizontalBox();
    private Box passwordBox = Box.createHorizontalBox();
    private Box identityBox = Box.createHorizontalBox();
    private Box ButtonBox = Box.createHorizontalBox();

    private DBUtil dbUtil = new DBUtil();
    private UserRun userDao = new UserRun();

    public void init() {
        jFrame.setBounds(500,350,500,300);
        jFrame.setResizable(false);

        identityGroup.add(identityButton1);
        identityGroup.add(identityButton2);

        lableBox.add(label);
        usernameBox.add(Box.createHorizontalStrut(80));
        usernameBox.add(UsernameLable);
        usernameBox.add(Box.createHorizontalStrut(20));
        usernameBox.add(UsernameText);
        usernameBox.add(Box.createHorizontalStrut(120));
        passwordBox.add(Box.createHorizontalStrut(80));
        passwordBox.add(PasswordLable);
        passwordBox.add(Box.createHorizontalStrut(20));
        passwordBox.add(PasswordText);
        passwordBox.add(Box.createHorizontalStrut(120));
        identityBox.add(Box.createHorizontalStrut(80));
        identityBox.add(identityLabel);
        identityBox.add(Box.createHorizontalStrut(20));
        identityBox.add(identityButton1);
        identityBox.add(identityButton2);
        identityBox.add(Box.createHorizontalStrut(120));
        ButtonBox.add(logIn);
        ButtonBox.add(Box.createHorizontalStrut(50));
        ButtonBox.add(register);

        mainBox.add(Box.createVerticalStrut(20));
        mainBox.add(lableBox);
        mainBox.add(Box.createVerticalStrut(40));
        mainBox.add(usernameBox);
        mainBox.add(Box.createVerticalStrut(20));
        mainBox.add(passwordBox);
        mainBox.add(Box.createVerticalStrut(20));
        mainBox.add(identityBox);
        mainBox.add(Box.createVerticalStrut(20));
        mainBox.add(ButtonBox);
        mainBox.add(Box.createVerticalStrut(20));

        jFrame.add(mainBox);
        jFrame.setVisible(true);

        logIn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取用户输入的数据
                String username = UsernameText.getText().trim();
                String password = PasswordText.getText().trim();
                String identity = null;
                if(identityButton1.isSelected()) {
                    identity = "user";
                }
                else {
                    identity = "manager";
                }

                //检查合法性
                if(StringUtil.isEmpty(username)) {
                    JOptionPane.showMessageDialog(null,"账号不能为空");
                    return;
                }
                if(StringUtil.isEmpty(password)) {
                    JOptionPane.showMessageDialog(null,"密码不能为空");
                    return;
                }

                //将数据打包为一个对象
                User user = new User(username,password,identity);

                //连接数据库进行操作
                Connection conn = null;
                try {
                    conn = dbUtil.getConnection();
                    User currentUser = userDao.login(conn,user);

                    //判断是否登录成功
                    if(currentUser.getId() != 0) {
                        //JOptionPane.showMessageDialog(null,"登录成功");
                        if(identityButton1.isSelected()) {
                            jFrame.setVisible(false);
                            new user_window().init(username,password);
                        }
                        else if(identityButton2.isSelected()) {
                            jFrame.setVisible(false);
                            new admin_window().init(username,password);
                        }

                    }
                    else {
                        JOptionPane.showMessageDialog(null,"账号或密码错误");
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

            }
        });
        register.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new com.view.log_inMenu.register().init();
            }
        });
    }

    public static void main(String[] args) {
        new log_in_window().init();
    }
}
